/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveWebServicesV12Helpers
{
	public class MessageContent
	{
		private GrooveWebServicesV12Helpers.Account m_Account;
		private GrooveWebServicesV12Helpers.MessageAttachment[] m_MessageAttachments;
		private GrooveWebServicesV12Helpers.MessageAttachment m_VoiceMemoMessageAttachment;
		
		private GrooveMessagesWebService.MessageContent m_MessageContent;


		public MessageContent(GrooveWebServicesV12Helpers.Account i_Account, GrooveMessagesWebService.MessageContent i_MessageContent)
		{
			m_Account = i_Account;
			m_MessageContent = i_MessageContent;
		}

		public MessageContent(GrooveWebServicesV12Helpers.Account i_Account, string i_Body)
			: this(i_Account, i_Body, null, null)
		{
		}

		public MessageContent(GrooveWebServicesV12Helpers.Account i_Account, string i_Body, GrooveWebServicesV12Helpers.MessageAttachment i_VoiceMemo)
			: this(i_Account, i_Body, i_VoiceMemo, null)
		{
		}

		public MessageContent(GrooveWebServicesV12Helpers.Account i_Account, string i_Body, GrooveWebServicesV12Helpers.MessageAttachment[] i_MessageAttachments)
			: this(i_Account, i_Body, null, i_MessageAttachments)
		{
		}

		public MessageContent(GrooveWebServicesV12Helpers.Account i_Account, string i_Body, GrooveWebServicesV12Helpers.MessageAttachment i_VoiceMemo, GrooveWebServicesV12Helpers.MessageAttachment[] i_MessageAttachments)
		{
			m_Account = i_Account;
			m_MessageContent = new GrooveMessagesWebService.MessageContent();

			//  Populate message content
			m_MessageContent.Body = i_Body;

			if (i_VoiceMemo != null)
				m_MessageContent.VoiceMemo = i_VoiceMemo.RawMessageAttachment;

			if (i_MessageAttachments != null)
			{
				System.Collections.ArrayList MessageAttachmentArrayList = new System.Collections.ArrayList();
				foreach (GrooveWebServicesV12Helpers.MessageAttachment MessageAttachment in i_MessageAttachments)
					MessageAttachmentArrayList.Add(MessageAttachment.RawMessageAttachment);

				m_MessageContent.AttachmentList = (GrooveMessagesWebService.MessageAttachment[])MessageAttachmentArrayList.ToArray(typeof(GrooveMessagesWebService.MessageAttachment));
			}
		}

		#region MessageContent Properties And Methods

		public GrooveWebServicesV12Helpers.Account Account
		{
			get
			{
				return m_Account;
			}
		}

		internal GrooveMessagesWebService.MessageContent RawMessageContent
		{
			get
			{
				return m_MessageContent;
			}
		}

		public GrooveWebServicesV12Helpers.MessageAttachment[] Attachments
		{
			get
			{
				if (m_MessageAttachments == null)
				{
					//  Create array list of MessageAttachments
					System.Collections.ArrayList MessageAttachmentArrayList = new System.Collections.ArrayList();
					foreach (GrooveMessagesWebService.MessageAttachment MessageAttachment in m_MessageContent.AttachmentList)
					{
						GrooveWebServicesV12Helpers.MessageAttachment NewMessageAttachment = new GrooveWebServicesV12Helpers.MessageAttachment
							(
							this,
							MessageAttachment
							);

						MessageAttachmentArrayList.Add(NewMessageAttachment);
					}

					//  Convert array list to array
					m_MessageAttachments = (GrooveWebServicesV12Helpers.MessageAttachment[])MessageAttachmentArrayList.ToArray(typeof(GrooveWebServicesV12Helpers.MessageAttachment));
				}

				return m_MessageAttachments;
			}
		}

		public string Body
		{
			get
			{
				return m_MessageContent.Body;
			}
		}

		public GrooveWebServicesV12Helpers.MessageAttachment VoiceMemo
		{
			get
			{
				if (m_VoiceMemoMessageAttachment == null)
				{
					m_VoiceMemoMessageAttachment = new GrooveWebServicesV12Helpers.MessageAttachment
						(
						this,
						m_MessageContent.VoiceMemo
						);
				}

				return m_VoiceMemoMessageAttachment;
			}
		}

		#endregion

	}
}
